package SEApp.dialogs
{
	import SEApp.dialogs.se_dialog;
	
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_list;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class se_albumsListDialog extends se_dialog {
		private var _doSelect	: Function;
		protected var _list		: uif_list;
		
		public function se_albumsListDialog(parent:uif_component) {
			super(parent, 0, 0, 300, 402, true, 1);
			title.text = 'Выберите альбом';
			var inner: Rectangle = getInner();
			_list = new uif_list(this, inner.x, inner.y, inner.width, inner.height, 'list');
			_list.addEvent(Event.CHANGE, doSelectAlbum);
			appObj.externalServer.getAlbums(appObj.externalServer.viewer_id, function(data: Array): void {
				_list.attachList(data, function(area: uif_ecomponent, item: Object, index: int): uif_component {
					var inner: Rectangle = area.getInner();
					var ctrl: uif_label = new uif_label(area, inner.x, inner.y + index * 25, inner.width, 25, 'listItem');
					ctrl.text = data[index].title; 
					return ctrl;
				});
			});
		}
		
		protected function get appObj(): Object {
			return app as Object;
		}
		
		protected function doSelectAlbum(e: Event): void {
			if (_doSelect != null) {
				new uif_Timer(100, null, function(te: Event): void {
					_doSelect((e.target as uif_list).list[(e.target as uif_list).selectIndex]);
				}, 1);
			}
		}
		
		public function showSelect(postFunc: Function): void {
			_doSelect = null;
			_list.selectIndex = -1;
			_doSelect = postFunc;
			show();
		} 
	}
}